{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/57_cartoee_blend.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) and [cartopy](https://scitools.org.uk/cartopy/docs/latest/installing.html#installing) if needed. Keep in mind that cartopy can be challenging to install. If you are unable to install cartopy on your computer, you can try Google Colab with this the [notebook example](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/notebooks/cartoee_colab.ipynb). \n",
    "\n",
    "See below the commands to install cartopy and geemap using conda/mamba:\n",
    "\n",
    "```\n",
    "conda create -n carto python=3.8\n",
    "conda activate carto\n",
    "conda install mamba -c conda-forge\n",
    "mamba install cartopy scipy -c conda-forge\n",
    "mamba install geemap -c conda-forge\n",
    "jupyter notebook\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install cartopy scipy\n",
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "# Creating publication-quality maps with multiple Earth Engine layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "from geemap import cartoee\n",
    "import cartopy.crs as ccrs\n",
    "\n",
    "%pylab inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Create an interactive map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "image = (\n",
    "    ee.ImageCollection(\"MODIS/MCD43A4_006_NDVI\")\n",
    "    .filter(ee.Filter.date(\"2018-04-01\", \"2018-05-01\"))\n",
    "    .select(\"NDVI\")\n",
    "    .first()\n",
    ")\n",
    "\n",
    "vis_params = {\n",
    "    \"min\": 0.0,\n",
    "    \"max\": 1.0,\n",
    "    \"palette\": [\n",
    "        \"FFFFFF\",\n",
    "        \"CE7E45\",\n",
    "        \"DF923D\",\n",
    "        \"F1B555\",\n",
    "        \"FCD163\",\n",
    "        \"99B718\",\n",
    "        \"74A901\",\n",
    "        \"66A000\",\n",
    "        \"529400\",\n",
    "        \"3E8601\",\n",
    "        \"207401\",\n",
    "        \"056201\",\n",
    "        \"004C00\",\n",
    "        \"023B01\",\n",
    "        \"012E01\",\n",
    "        \"011D01\",\n",
    "        \"011301\",\n",
    "    ],\n",
    "}\n",
    "Map.setCenter(-7.03125, 31.0529339857, 2)\n",
    "Map.addLayer(image, vis_params, \"MODIS NDVI\")\n",
    "\n",
    "countries = ee.FeatureCollection(\"users/giswqs/public/countries\")\n",
    "style = {\"color\": \"00000088\", \"width\": 1, \"fillColor\": \"00000000\"}\n",
    "Map.addLayer(countries.style(**style), {}, \"Countries\")\n",
    "\n",
    "ndvi = image.visualize(**vis_params)\n",
    "blend = ndvi.blend(countries.style(**style))\n",
    "\n",
    "Map.addLayer(blend, {}, \"Blend\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "## Plot an image with the default projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# specify region to focus on\n",
    "bbox = [180, -88, -180, 88]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15, 10))\n",
    "\n",
    "# plot the result with cartoee using a PlateCarre projection (default)\n",
    "ax = cartoee.get_map(blend, region=bbox)\n",
    "cb = cartoee.add_colorbar(ax, vis_params=vis_params, loc=\"right\")\n",
    "\n",
    "ax.set_title(label=\"MODIS NDVI\", fontsize=15)\n",
    "\n",
    "# ax.coastlines()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "## Plot an image with a different projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15, 10))\n",
    "\n",
    "projection = ccrs.EqualEarth(central_longitude=-180)\n",
    "\n",
    "# plot the result with cartoee using a PlateCarre projection (default)\n",
    "ax = cartoee.get_map(blend, region=bbox, proj=projection)\n",
    "cb = cartoee.add_colorbar(ax, vis_params=vis_params, loc=\"right\")\n",
    "\n",
    "ax.set_title(label=\"MODIS NDVI\", fontsize=15)\n",
    "\n",
    "# ax.coastlines()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernel_info": {
   "name": "python3"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
